<template>
  <div>
    <el-form ref="formData" :model="formData" :rules="rules" size="medium" label-width="100px">
      <el-form-item label="用户" prop="username">
        <el-input v-model="formData.username" placeholder="请输入用户" readonly :style="{width: '100%'}">
        </el-input>
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker v-model="formData.startTime" format="yyyy-MM-dd" value-format="yyyy-MM-dd"
                        :style="{width: '100%'}" placeholder="请选择开始时间" clearable></el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker v-model="formData.endTime" format="yyyy-MM-dd" value-format="yyyy-MM-dd"
                        :style="{width: '100%'}" placeholder="请选择结束时间" clearable></el-date-picker>
      </el-form-item>
      <el-form-item label="请假原因" prop="reason">
        <el-input v-model="formData.reason" type="textarea" placeholder="请输入请假原因"
                  :autosize="{minRows: 4, maxRows: 4}" :style="{width: '100%'}"></el-input>
      </el-form-item>
    </el-form>
    <el-form ref="param" :model="param" :rules="rules" size="medium" label-width="100px">
      <el-form-item label="处理人1" prop="param1">
        <el-select v-model="param.param1" placeholder="请选择处理人1" clearable :style="{width: '100%'}">
          <el-option v-for="(item, index) in param1Options" :key="index" :label="item.label"
                     :value="item.value" :disabled="item.disabled"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="处理人2" prop="param2">
        <el-select v-model="param.param2" placeholder="请选择处理人2" clearable :style="{width: '100%'}">
          <el-option v-for="(item, index) in param2Options" :key="index" :label="item.label"
                     :value="item.value" :disabled="item.disabled"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
      <div slot="footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="handleConfirm">确定</el-button>
      </div>
  </div>
</template>
<script>

import {startProcess} from '@/api/bpmn/leave'
import assigneeService from '@/api/bpmn/assigneeService'


export default {
  name: '请假表单',
  inheritAttrs: false,
  components: {},
  props: [],
  data() {
    return {
      formData: {
        userId: undefined,
        startTime: null,
        endTime: null,
        reason: undefined,
      },
      param: {
        param1: undefined,
        param2: undefined,
      },
      rules: {
        userId: [{
          required: true,
          message: '请输入用户',
          trigger: 'blur'
        }],
        startTime: [{
          required: true,
          message: '请选择开始时间',
          trigger: 'change'
        }],
        endTime: [{
          required: true,
          message: '请选择结束时间',
          trigger: 'change'
        }],
        reason: [{
          required: true,
          message: '请输入请假原因',
          trigger: 'blur'
        }],
        param1: [{
          required: true,
          message: '请选择处理人1',
          trigger: 'change'
        }],
        param2: [{
          required: true,
          message: '请选择处理人2',
          trigger: 'change'
        }],
      },
      param1Options: undefined,
      param2Options: undefined,
    }
  },
  computed: {},
  watch: {},
  created() {
    this.initUserId()
    this.getOptions()
  },
  mounted() {},
  methods: {
    close() {
      this.$emit('update:visible', false)
    },
    handleConfirm() {
      this.$refs['formData'].validate(valid => {
        if (!valid){
          return
        }else {
          this.$refs['param'].validate(valid => {
            if (!valid) {
              return
            }
            else {
              this.startProcess()
              this.close()
            }
          })
        }
      })
    },
    /* 初始化用户id */
    initUserId(){
      this.formData.username = this.$store.getters.name
    },
    /* 获取选项 */
    getOptions(){
      assigneeService.selectOptionsByUser(null)
        .then(res => {
          this.param1Options = res;
          this.param2Options = res;
        })
    },
    startProcess(){
      let map = {
        form: this.formData,
        map: this.param
      }
      startProcess(map).then(res => {
        this.$message.success('提交成功')
        this.clear()
      }).catch(res => {
        throw new Error(res)
      })
    },
    clear(){
      this.$refs['formData'].resetFields()
      this.$refs['param'].resetFields()
    }


  }
}

</script>
<style>
</style>
